#include <QCoreApplication>
#include <QFileInfo>
#include <QDir>
#include <QDebug>

#include "datastorage.h"

DataStorage::DataStorage(QObject *parent) :
    QSettings(parent)
{

}

bool DataStorage::getStockAnalyzerData(QString str, QStringList &strlist)
{
    QString path = QCoreApplication::applicationDirPath() + "/data";
    QFileInfo fileInfo(QDir(path), "StockAnalyzer.ini");
    if(fileInfo.exists())
    {
        qDebug()<<"**********************";
        QSettings settings(fileInfo.absoluteFilePath(), QSettings::IniFormat);
        settings.beginGroup(str);
        int count = settings.value("count").toInt();
        for(int index = 0; index < count; index++)
        {
            QString stockCode = settings.value(QString::number(index)).toString();
            strlist.append(stockCode);
        }

        return true;
    }
    else
    {
        return false;
    }
}

void DataStorage::saveStockAnalyzerData(QString str, QStringList &strlist)
{
    QString path = QCoreApplication::applicationDirPath() + "/data";
    QFile file(path + "/StockAnalyzer.ini");
    if(file.exists())
    {
        file.remove();
    }


    QSettings settings(QCoreApplication::applicationDirPath() +
                       "/data/StockAnalyzer.ini", QSettings::IniFormat);
    settings.beginGroup(str);

    int count = strlist.count();
    settings.setValue("count", count);

    for(int index = 0; index < count; index++)
    {
        settings.setValue(QString::number(index), strlist.at(index));
    }

    settings.endGroup();
}
